package com.wh.stressservice.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @Author xu
 * @Date 2025/2/10
 **/
@Configuration
public class DataSourceConfig {

    // MySQL 数据源配置
    @Primary
    @Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "mysqlJdbcTemplate")
    public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    // Access 数据源配置
    @Bean(name = "accessDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.access")
    public DataSource accessDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "accessJdbcTemplate")
    public JdbcTemplate accessJdbcTemplate(@Qualifier("accessDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
